package com.uzai.console.service.tb.tbtoken.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.tb.tbtoken.*;
import com.uzai.console.entity.TbActivityRegToken;
import com.uzai.console.entity.TbToken;
import com.uzai.console.entity.TbTokenCookie;
import com.uzai.console.mapper.TbActivityRegTokenMapper;
import com.uzai.console.mapper.TbTokenCookieMapper;
import com.uzai.console.mapper.TbTokenMapper;
import com.uzai.console.service.tb.tbridpid.TbRidPidService;
import com.uzai.console.service.tb.tbtoken.TbTokenService;
import com.uzai.console.vo.base.SelectIdVo;
import com.uzai.console.vo.tb.tbactivityregtoken.TbActivityRegTokenVo;
import com.uzai.console.vo.tb.tbtoken.TbTokenAuthUrlVo;
import com.uzai.console.vo.tb.tbtoken.TbTokenInfoVo;
import com.uzai.console.vo.tb.tbtoken.TbTokenPageVo;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/***
 * 运营商基本配置
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class TbTokenServiceImpl implements TbTokenService {

    Logger logger = LoggerFactory.getLogger(TbTokenServiceImpl.class);

    @Autowired
    private TbTokenMapper tbTokenMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;
    @Autowired
    private TbActivityRegTokenMapper tbActivityRegTokenMapper;
    @Autowired
    private TbTokenCookieMapper tbTokenCookieMapper;
    @Autowired
    private TbRidPidService tbRidPidService;
    /**
     * 查询淘宝联盟列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbTokenQuery
     * @return null
     */
    public Page<TbTokenPageVo> findTbTokenList(TbTokenQuery tbTokenQuery){
        Integer now = DateUtil.getNowTime();

        //新建返回到页面的LIST对象
        List<TbTokenPageVo> tbTokenPageVoList = new ArrayList<>();

        //执行分页查询对象
        Page<TbToken> page = new Page<>();
        tbTokenQuery.convert(page);
        ArrayList<TbToken> tbOrderList =  tbTokenMapper.findByList(page, tbTokenQuery);
        if(tbOrderList != null && tbOrderList.size() > 0){
            for (TbToken tbToken : tbOrderList){
                TbTokenPageVo tbTokenPageVo = new TbTokenPageVo();
                BeanUtils.copyProperties(tbToken, tbTokenPageVo);
                if(StringUtils.isNotBlank(tbToken.getPids())){
                    tbTokenPageVo.setPidList(JSONObject.parseArray(tbToken.getPids(), String.class));
                }

                //到期时间
                Integer expiretime = Tools.getInteger(tbToken.getExptime());
                if(expiretime.intValue() <= now.intValue()){
                    tbTokenPageVo.setExpireStatus(0);  //已到期
                }else if(expiretime.intValue() > now && expiretime.intValue() <= now + 7*24*3600){
                    tbTokenPageVo.setExpireStatus(2);  //即将到期
                }else{
                    tbTokenPageVo.setExpireStatus(1);  //未到期
                }

                tbTokenPageVoList.add(tbTokenPageVo);
            }
        }

        //返回到页面分页对象
        Page<TbTokenPageVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(tbTokenPageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }


     /**
     * 设置炮灰
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbTokenSetPhDto
     * @return null
     */
    public void setPh(TbTokenSetPhDto tbTokenSetPhDto){
        if(tbTokenSetPhDto.getId() == null || tbTokenSetPhDto.getIsPh() == null || tbTokenSetPhDto.getPhOrderCount() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        TbToken tbToken = tbTokenMapper.selectById(tbTokenSetPhDto.getId(), tbTokenSetPhDto.getMerId());

        if(tbToken == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该淘宝联盟账号不存在");
        }

        tbToken.setPhOrderCount(tbTokenSetPhDto.getPhOrderCount());
        tbToken.setIsPh(tbTokenSetPhDto.getIsPh());
        //启动炮灰，需要将辅助关闭
        if(tbTokenSetPhDto.getIsPh().intValue() == 1){
            tbToken.setIsAux(0);
        }

        tbTokenMapper.update(tbToken);

    }

     /**
     * 设置辅助账号
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbTokenSetAuxDto
     * @return null
     */
    public void setAux(TbTokenSetAuxDto tbTokenSetAuxDto){
        if(tbTokenSetAuxDto.getId() == null || tbTokenSetAuxDto.getIsAux() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }
        TbToken tbToken = tbTokenMapper.selectById(tbTokenSetAuxDto.getId(), tbTokenSetAuxDto.getMerId());
        if(tbToken == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该淘宝联盟账号不存在");
        }
        tbToken.setIsAux(tbTokenSetAuxDto.getIsAux());
        //启动辅助，需要将炮灰关闭
        if(tbTokenSetAuxDto.getIsAux().intValue() == 1){
            tbToken.setIsPh(0);
        }
        tbTokenMapper.update(tbToken);

    }

     /***
     * 添加授权
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbTokenDto
     * @return null
     */
    public TbTokenAuthUrlVo addTbToken(TbTokenDto tbTokenDto){
        //刷新授权的链接，将运营商ID放入自定义参数state当中，授权刷新之后，淘宝调用回调函数返回授权信息
        String url = null;
        //淘宝联盟(返利)（0-淘宝返利联盟；1-饿了么联盟；2-饿了么刷新渠道id联盟；3-淘宝导购联盟）
        if(Tools.getInteger(tbTokenDto.getTurnlink()).intValue() == 1){ //导购
            String merId_type =  "v3" + tbTokenDto.getMerId() + "_3";
            url =  uzaiConsoleSysConfig.getTbTokenAuthorize_dg() + merId_type;

        }else{
            String merId_type =  "v3" + tbTokenDto.getMerId() + "_0";
            url =  uzaiConsoleSysConfig.getTbTokenAuthorize() + merId_type;
        }

        TbTokenAuthUrlVo tbTokenAuthUrlVo = new  TbTokenAuthUrlVo();
        tbTokenAuthUrlVo.setUrl(url);
        return tbTokenAuthUrlVo;
    }

     /***
     * 查询联盟详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbTokenIdDto
     * @return null
     */
    public TbTokenInfoVo findTbTokenInfo(TbTokenIdDto tbTokenIdDto){

        if(tbTokenIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }
        TbToken tbToken = tbTokenMapper.selectById(tbTokenIdDto.getId(), tbTokenIdDto.getMerId());
        if(tbToken == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该淘宝联盟账号不存在");
        }

        //返回页面对象
        TbTokenInfoVo tbTokenInfoVo = new TbTokenInfoVo();
        //将查询到的对象复制给返回页面对象
        BeanUtils.copyProperties(tbToken, tbTokenInfoVo);

        if(StringUtils.isNotBlank(tbToken.getPids())){
            tbTokenInfoVo.setPidList(JSONObject.parseArray(tbToken.getPids(), String.class));
        }

        return tbTokenInfoVo;
    }

    @Override
    public List<TbToken> listTbToken(Long merId, List<Long> ids) {
        if (Objects.isNull(merId) || CollectionUtil.isEmpty(ids)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<TbToken> condition = Wrappers.lambdaQuery(TbToken.class);
        condition.eq(TbToken::getMerId, merId)
                .in(TbToken::getId, ids);
        return tbTokenMapper.selectList(condition);
    }

     /***
     * 修改联盟PID
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbTokenUpdatePidDto
     * @return null
     */
    public void updatePid(TbTokenUpdatePidDto tbTokenUpdatePidDto){

        if(tbTokenUpdatePidDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        if(tbTokenUpdatePidDto.getSettleType() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "比价订单行为未选择方式");
        }

        if(StringUtils.isBlank(tbTokenUpdatePidDto.getDefPid())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请设置默认PID");
        }

        String[] defPidList = tbTokenUpdatePidDto.getDefPid().split("_");
        if(defPidList == null || defPidList.length != 4){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "PID格式不正确");
        }

        TbToken tbToken = tbTokenMapper.selectById(tbTokenUpdatePidDto.getId(), tbTokenUpdatePidDto.getMerId());
        if(tbToken == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该淘宝联盟账号不存在");
        }

        tbToken.setSettleType(tbTokenUpdatePidDto.getSettleType());
        tbToken.setDefPid(tbTokenUpdatePidDto.getDefPid());
        tbToken.setMmUserId(Long.valueOf(defPidList[1])); //获取mmUserId=defPid的第二位
        tbToken.setPids(JSONObject.toJSONString(tbTokenUpdatePidDto.getPidList()));
        tbToken.setPidWlist(tbTokenUpdatePidDto.getPidWlist());
        tbTokenMapper.update(tbToken);

        //pid队列缓存key
        String tbPidsQueueKey = String.format(RedisCacheKeyEnum.TB_PIDS_QUEUE.getKey(), tbToken.getMerId(), tbToken.getUserId());
        if (Tools.getInteger(tbToken.getTurnlink()).intValue() == 1){ //导购联盟，队列缓存key在后面+1
            tbPidsQueueKey = tbPidsQueueKey + "1";
        }

        //设置redis
        //先删除pid队列
        redisTemplate.delete(tbPidsQueueKey);
        //把pidList添加redis队列当中
        List<String> pidList = tbTokenUpdatePidDto.getPidList();
        if(pidList != null && pidList.size() > 0){
            for (String pid : pidList){
                redisTemplate.opsForList().leftPushAll(tbPidsQueueKey, pid);
            }
        }
    }

     /***
     * 设置是否同步订单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbTokenIsSyncOrderDto
     * @return null
     */
    public void setIsSyncOrder(TbTokenIsSyncOrderDto tbTokenIsSyncOrderDto){
        if(tbTokenIsSyncOrderDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        TbToken tbToken = tbTokenMapper.selectById(tbTokenIsSyncOrderDto.getId(), tbTokenIsSyncOrderDto.getMerId());

        if(tbToken == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此联盟不存在");
        }

        tbToken.setIsSyncOrder(tbTokenIsSyncOrderDto.getIsSyncOrder());

        tbTokenMapper.update(tbToken);
    }

     /***
     * 删除
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbTokenIdDto
     * @return null
     */
    public void deleteTbToken(TbTokenIdDto tbTokenIdDto){
        if(tbTokenIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        TbToken tbToken = tbTokenMapper.selectById(tbTokenIdDto.getId(), tbTokenIdDto.getMerId());
        if(tbToken != null){
            if(tbToken.getType().intValue() == 2){ //淘宝和饿了么共享联盟
                tbToken.setType(1); //修改成饿了么独享联盟
                tbTokenMapper.updateSelective(tbToken);
            }else{
                tbTokenMapper.deleteById(tbTokenIdDto.getId(), tbTokenIdDto.getMerId());
                //删除pid队列缓存key
                String tbPidsQueueKey = String.format(RedisCacheKeyEnum.TB_PIDS_QUEUE.getKey(), tbToken.getMerId(), tbToken.getUserId());
                if (Tools.getInteger(tbToken.getTurnlink()).intValue() == 1){ //导购联盟，队列缓存key在后面+1
                    tbPidsQueueKey = tbPidsQueueKey + "1";
                }
                redisTemplate.delete(tbPidsQueueKey);
            }
        }
    }

    @Override
    public TbToken getOneValidTbToken(Long merId) {
        long currentSecond = System.currentTimeMillis() / 1000L;

        LambdaQueryWrapper<TbToken> condition = Wrappers.lambdaQuery(TbToken.class);
        condition.eq(TbToken::getMerId, merId)
                .gt(TbToken::getExptime, currentSecond);

        List<TbToken> tbTokens = tbTokenMapper.selectList(condition);
        if (ObjectUtils.isEmpty(tbTokens)) {
            throw new BusinessException("默认淘宝联盟账号未找到");
        }
        return tbTokens.get(0);
    }

    @Override
    public Boolean update(TbTokenUpdateDto tbTokenUpdateDto) {

        Long id = tbTokenUpdateDto.getId();
        Long merId = tbTokenUpdateDto.getMerId();

        TbToken tbToken = tbTokenMapper.selectById(id, merId);

        if (Objects.isNull(tbToken)) {
            throw new BusinessException("淘宝联盟账号不存在");
        }
        tbToken.setRemark(tbTokenUpdateDto.getRemark());
        tbToken.setUpdateTime((int) cn.hutool.core.date.DateUtil.currentSeconds());
        tbToken.setOrderTailBind(Tools.getInteger(tbTokenUpdateDto.getOrderTailBind()));
        tbToken.setSidBind(Tools.getInteger(tbTokenUpdateDto.getSidBind()));
        tbTokenMapper.update(tbToken);
        return true;
    }

    /**
     * 查询所有淘宝联盟列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbTokenQuery
     * @return null
     */
    public List<TbTokenInfoVo> findTbTokenListByAll(TbTokenQuery tbTokenQuery){
        Integer now = DateUtil.getNowTime();

        //新建返回到页面的LIST对象
        List<TbTokenInfoVo> tbTokenInfoVoList = new ArrayList<>();

        //执行分页查询对象

        ArrayList<TbToken> tbOrderList =  tbTokenMapper.findByList(tbTokenQuery);
        if(tbOrderList != null && tbOrderList.size() > 0){
            for (TbToken tbToken : tbOrderList){
                TbTokenInfoVo tbTokenInfoVo = new TbTokenInfoVo();
                BeanUtils.copyProperties(tbToken, tbTokenInfoVo);
                if(StringUtils.isNotBlank(tbToken.getPids())){
                    tbTokenInfoVo.setPidList(JSONObject.parseArray(tbToken.getPids(), String.class));
                }

                //到期时间
                Integer expiretime = Tools.getInteger(tbToken.getExptime());
                if(expiretime.intValue() <= now.intValue()){
                    tbTokenInfoVo.setExpireStatus(0);  //已到期
                }else if(expiretime.intValue() > now && expiretime.intValue() <= now + 7*24*3600){
                    tbTokenInfoVo.setExpireStatus(2);  //即将到期
                }else{
                    tbTokenInfoVo.setExpireStatus(1);  //未到期
                }
                tbTokenInfoVoList.add(tbTokenInfoVo);
            }
        }

        //返回到页面对象
        return tbTokenInfoVoList;
    }

    /**
     * 查询淘宝联盟下拉选择列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbTokenQuery
     * @return null
     */
    public List<SelectIdVo> findTbTokenSelectList(TbTokenQuery tbTokenQuery){
        //tbTokenQuery.setIsPh(0); //是否为炮灰账号(0否，1是)
        List<TbToken> tbTtkenList = tbTokenMapper.findByList(tbTokenQuery);
        //新建返回页面对象
        List<SelectIdVo> tbTokenVoList = new ArrayList<SelectIdVo>();
        if(tbTtkenList != null ){
            for (TbToken tbToken : tbTtkenList) {
                SelectIdVo  SelectIdVo = new SelectIdVo();
                SelectIdVo.setKey(tbToken.getId());

                //备注
                String remark = tbToken.getRemark();
                if(Tools.getInteger(tbToken.getTurnlink()).intValue() == 1){
                    if(StringUtils.isNotBlank(tbToken.getRemark())){
                        remark =  "导购_" + remark;
                    }else{
                        remark =   "导购";
                    }
                }else{
                    if(StringUtils.isNotBlank(tbToken.getRemark())){
                        remark =  "返利_" + remark;
                    }else{
                        remark =   "返利";
                    }
                }
                String value = tbToken.getUserName();
                if(StringUtils.isNotBlank(remark)){
                    value = value + "（" + remark + "）";
                }
                SelectIdVo.setValue(value);

                tbTokenVoList.add(SelectIdVo);
            }
        }
        return tbTokenVoList;
    }


    /**
     * 查询淘宝联盟618赛马列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbTokenQuery
     * @return null
     */
    public Page<TbActivityRegTokenVo> findTbActivityRegTokenList(TbTokenQuery tbTokenQuery){
        //新建返回到页面的LIST对象
        List<TbActivityRegTokenVo> tbTokenPageVoList = new ArrayList<>();

        //执行分页查询对象
        Page<TbToken> page = new Page<>();
        tbTokenQuery.convert(page);
        ArrayList<TbToken> tbOrderList =  tbTokenMapper.findByList(page, tbTokenQuery);
        if(tbOrderList != null && tbOrderList.size() > 0){
            for (TbToken tbToken : tbOrderList){
                TbActivityRegTokenVo tbActivityRegTokenVo = new TbActivityRegTokenVo();
                BeanUtils.copyProperties(tbToken, tbActivityRegTokenVo);
                tbTokenPageVoList.add(tbActivityRegTokenVo);
                tbActivityRegTokenVo.setId(tbToken.getId());
                tbActivityRegTokenVo.setMmUserId(tbToken.getMmUserId());
                tbActivityRegTokenVo.setUserId(tbToken.getUserId());
                tbActivityRegTokenVo.setUserName(tbToken.getUserName());

                //查询已经报名的赛马信息
                TbActivityRegToken tbActivityRegToken = tbActivityRegTokenMapper.selectByMermberId(String.valueOf(tbToken.getMmUserId()));
                if(tbActivityRegToken == null || Tools.getInteger(tbActivityRegToken.getStatus()) == 0){
                    tbActivityRegTokenVo.setStatus(0); //未报名
                    //从cookie中判断是一键报名，还是报名链接
                    TbTokenCookie tbTokenCookie = tbTokenCookieMapper.selectByUserId(String.valueOf(tbToken.getUserId()));
                    if(tbTokenCookie != null && StringUtils.isNotBlank(tbTokenCookie.getCookie())){
                        tbActivityRegTokenVo.setCookie(tbTokenCookie.getCookie());
                        tbActivityRegTokenVo.setReplyType(1);
                    }else{
                        tbActivityRegTokenVo.setReplyType(0);
                    }
                }else{
                    tbActivityRegTokenVo.setStatus(1);
                    tbActivityRegTokenVo.setRedpackCount(tbActivityRegToken.getRedpackCount());
                    tbActivityRegTokenVo.setRegTime(tbActivityRegToken.getRegTime());
                }
            }
        }

        //返回到页面分页对象
        Page<TbActivityRegTokenVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(tbTokenPageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 批量创建pid列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param createPidDto
     * @return null
     */
    public List<String> createPidList(CreatePidDto createPidDto){
        List<String> pidList = new ArrayList<>();

        if(createPidDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择联盟");
        }

        if(createPidDto.getSiteId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入网站id");
        }

        if(StringUtils.isBlank(createPidDto.getAdzoneName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入推广位名称");
        }

        if(createPidDto.getAdzoneName().length() > 20){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "推广位不能超过20个字符");
        }

        if(createPidDto.getType() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入类型");
        }

        if(createPidDto.getNum() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入数量");
        }

        TbToken tbToken = tbTokenMapper.selectById(createPidDto.getId(), createPidDto.getMerId());
        if(tbToken == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该淘宝联盟不存在");
        }

        if(tbToken.getExptime() < DateUtil.getNowTime()){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该淘宝联盟已过期");
        }

        //封装推广位名称列表
        List<String> adzoneNameList = new ArrayList<>();

        if(createPidDto.getType() == 0 ){//自增长生成
            //在1000-9000中随机获取一个数字，作为本次开始数字
            int startIndex = RandomUtil.randomInt(1000, 9000);
            //循环依次生成推广位名称
            for(int i=0;  i<createPidDto.getNum(); i++){
                startIndex = startIndex + i;
                String adzoneName = createPidDto.getAdzoneName() + startIndex;
                adzoneNameList.add(adzoneName);
            }
        }else{//随机生成
            int lenth = adzoneNameList.size();
            while (lenth < createPidDto.getNum()){
                //随机生成4位数字和字符的随机字符串
                String randomIndex = RandomUtil.randomStringUpper(4);
                String adzoneName = createPidDto.getAdzoneName() + randomIndex;
                if(!adzoneNameList.contains(adzoneName)){
                    adzoneNameList.add(adzoneName);
                }
                lenth = adzoneNameList.size();
            }
        }

        String appkey = uzaiConsoleSysConfig.getTbAppkey();
        String appsecret = uzaiConsoleSysConfig.getTbAppsecret();

        if(Tools.getInteger(tbToken.getTurnlink()) == 1){ //导购
            appkey = uzaiConsoleSysConfig.getTbAppkeyDg();
            appsecret = uzaiConsoleSysConfig.getTbAppsecretDg();
        }

        //生成淘宝client
        TaobaoClient client = new DefaultTaobaoClient(uzaiConsoleSysConfig.getTbApiUrl(), appkey, appsecret);
        for (String adzoneName : adzoneNameList) {
            String pid = tbRidPidService.createPid(client, tbToken.getAccessToken(), createPidDto.getSiteId(), adzoneName);
            pidList.add(pid);
        }

        return pidList;
    }


}
